﻿using System;
using System.IO;
using System.Text;

namespace BGN.PlugIn.Client.Msg
{
    public static class MsgHelper
    {
        /// <summary>
        /// 保存消息并返回文件的Url  Path
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static String SaveMsg(String msg, String sender, String userName, String datetime)
        {
            String msgFolderPath = GetMsgPath(userName);
            System.IO.Directory.CreateDirectory(msgFolderPath);
            String msgFilePath = Path.Combine(msgFolderPath, String.Format("{0}.html", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(msgFilePath, false, Encoding.UTF8))
            {
                String sPreMsg = "{0}收到【{1}】的消息:<br>{2}";
                sw.Write(String.Format(sPreMsg, datetime, sender, msg));
                sw.Dispose();
            }
            return msgFilePath;
        }

        /// <summary>
        /// 为电脑短信添加已读用户
        /// </summary>
        /// <param name="msgID"></param>
        /// <param name="userName"></param>
        public static void AddReader(String msgID, String userName)
        {
            String webServiceUrl = SInfoWebService.GetWebserviceUrl();
            //通过web服务调用接口更新sinfo系统
            DreamCube.Foundation.Basic.Utility.MyWeb.InvokeWebService(webServiceUrl, "AddMsgReader", new Object[] { msgID, userName });
        }

        private static String MsgHtmlTemplate
        {
            get
            {
                return "<html xmlns='http://www.w3.org/1999/xhtml'>" +
                        "<head>" +
                            "<title></title>" +
                        "</head>" +
                        "<body>" +
                            "<div style='width:666px'>{0}</div>" +
                        "</body>" +
                        "</html>";
            }
        }

        private static String GetMsgPath(String userName)
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), String.Format(@"SisenMESS\{0}\{1}", userName, "SinfoMsg"));
        }
    }
}
